package com.watermeter.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.oa.android.R;
import com.watermeter.android.activity.DownloadRouteActivity;
import com.watermeter.android.entry.DnowDate;
import com.watermeter.android.entry.Rows;

import java.util.List;

/**
 * Created by zouqh on 2016/5/23.
 */
public class RouteAdapter extends BaseAdapter {
    private Context context;
    private List<Rows> rowses;
    private String dateTiem;
    private List<DnowDate> dateList;

    public RouteAdapter(Context context, List<Rows> rowses, String dateTiem, List<DnowDate> dateList) {
        this.context = context;
        this.rowses = rowses;
        this.dateTiem = dateTiem;
        this.dateList = dateList;
    }

    @Override
    public int getCount() {
        return rowses.size();
    }

    @Override
    public Object getItem(int position) {
        return rowses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHodler viewHodler;
        if (convertView == null) {
            viewHodler = new ViewHodler();
            convertView = LayoutInflater.from(context).inflate(R.layout.route_item, null);
            viewHodler.code = (TextView) convertView.findViewById(R.id.code);
            viewHodler.name = (TextView) convertView.findViewById(R.id.routeName);
            viewHodler.person = (TextView) convertView.findViewById(R.id.CBPerson);
            viewHodler.down = (TextView) convertView.findViewById(R.id.donwDate);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHodler) convertView.getTag();
        }

        viewHodler.name.setText("线路名称：" + rowses.get(position).getNAME());
        viewHodler.code.setText("线路编码：" + rowses.get(position).getID());
        viewHodler.person.setText("抄表人员：" + rowses.get(position).getCBY());
        if(DownloadRouteActivity.activity.dateList!=null&&DownloadRouteActivity.activity.dateList.size()>0){
            for (int i=0;i< DownloadRouteActivity.activity.dateList.size();i++){
                if(DownloadRouteActivity.activity.dateList.get(i).getTouteId().equals(rowses.get(position).getID())
                        && DownloadRouteActivity.activity.dateList.get(i).getDateTime().equals(DownloadRouteActivity.activity.time)){
                    viewHodler.down.setText("已下载");
                    viewHodler.down.setBackgroundResource(R.drawable.button_press);
                    viewHodler.down.setEnabled(false);
                    break;
                }else{
                    viewHodler.down.setEnabled(true);
                    viewHodler.down.setText("未下载");
                    viewHodler.down.setBackgroundResource(R.drawable.button_img);
                }
            }
        }else{
            viewHodler.down.setEnabled(true);
            viewHodler.down.setBackgroundResource(R.drawable.button_img);
            viewHodler.down.setText("未下载");
        }

        viewHodler.down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownloadRouteActivity.activity.getDown(rowses.get(position).getID(), position);
            }
        });
        return convertView;
    }

    class ViewHodler {
        TextView name;
        TextView code;
        TextView person;
        TextView down;
    }
}
